package com.uton.rfidcontroller.view.adapter.task;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.databinding.library.baseAdapters.BR;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.databinding.ItemAlreadyDoTaskListBinding;
import com.uton.rfidcontroller.model.CarDealerListModel;

import java.util.List;


public class TaskListAlreadyDoAdapter extends BaseRecyclerAdapter<TaskListAlreadyDoAdapter.SimpleAdapterViewHolder> {
    private List<CarDealerListModel> data;
    private Context mContext;

    public TaskListAlreadyDoAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }

    public void setData(List<CarDealerListModel> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        ItemAlreadyDoTaskListBinding mItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_already_do_task_list, parent, false);

        SimpleAdapterViewHolder mHolder = new SimpleAdapterViewHolder(mItemBinding.getRoot(), true);
        mHolder.setBinding(mItemBinding);//把mItemBinding设置给ViewHolder

        return mHolder;
    }

    @Override
    public void onBindViewHolder(final TaskListAlreadyDoAdapter.SimpleAdapterViewHolder holder, int position, boolean isItem) {
        //        通过holder.getBinding()得到Binding Class
        holder.getBinding().setVariable(BR.model, data.get(holder.getAdapterPosition()));

        holder.getBinding().rlCarListItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.showShortToast("该功能正在开发中");
            }
        });

        holder.getBinding().executePendingBindings();//立即更新UI
    }


    public void insert(CarDealerListModel s, int position) {
        insert(data, s, position);
    }

    public void clear() {
        if (data != null) {
            clear(data);
        }
    }

    class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {

        private ItemAlreadyDoTaskListBinding binding;

        public ItemAlreadyDoTaskListBinding getBinding() {
            return binding;
        }

        public void setBinding(ItemAlreadyDoTaskListBinding binding) {
            this.binding = binding;
        }

        SimpleAdapterViewHolder(View itemView, boolean isItem) {
            super(itemView);
        }

    }

}